package com.dao.impl;

import com.dao.AdminDao;
import com.entity.Admin;
import com.entity.AdminExample;
import com.mapper.AdminMapper;
import com.page.PageInfo;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class AdminDaoImpl implements AdminDao {
    @Resource
    AdminMapper adminMapper;

    /**
     * 根据管理员编号查询一个管理员
     * @param adid  管理员编号
     * @return 管理员对象
     */
    public Admin getByAdid(String adid){
        return adminMapper.selectByPrimaryKey(adid);
    }
    public List<Admin> getAll(){
        return adminMapper.getAll();
    }
    public List<Admin> getAllByPage(PageInfo pageInfo){
        int currPage = pageInfo.getCurrPage();
        int pageSize = pageInfo.getPageSize();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", currPage);
        map.put("pageSize", pageSize);
        System.out.println(pageInfo.getCurrPage() + " "+ pageInfo.getPageSize());
        //调用示例查询得到当前的总记录数
        pageInfo.setItemCount((int) adminMapper.countByExample(new AdminExample()));
        return adminMapper.getAllByPage(map);
    }

    @Override
    public List<Admin> searchNameByPage(PageInfo pageInfo,String adname) {

        AdminExample adminExample = new AdminExample();
        int limit = pageInfo.getPageSize();
        int offset = (pageInfo.getCurrPage() - 1) * limit;
        adminExample.setOffset(offset);//就是原来的firstResult 就是通过（currPage - 1） * pageSize
        adminExample.setLimit(limit);
        //createCriteria 创建一个条件查询
        adminExample.createCriteria().andAdnameLike(adname);
        return adminMapper.selectByExample(adminExample);
    }

    /**
     * 根据主键查询
     * @param adid
     * @return
     */
    public Admin getAdminByAdid(String adid){
        return adminMapper.selectByPrimaryKey(adid);
    }

    /**
     * 添加一个实体
     * @param admin
     * @return
     */
    public int addAdmin(Admin admin){
        return adminMapper.insertSelective(admin);
    }
    /**
     * 修改实体,如果有条件 需要设置example
     */
    public int updateAdmin(Admin admin){
        return adminMapper.updateByPrimaryKeySelective(admin);
    }
    public int updateAdminByExample(Admin admin){
        return adminMapper.updateByExample(admin,null);
    }

    /**
     * 删除一个实体
     * @param adid
     * @return
     */
    public int deleteAdminByPrimayKey(String adid){
        return adminMapper.deleteByPrimaryKey(adid);
    }

    /**
     * 删除实体根据示例
     * @return
     */
    public int deleteAdminByExample(){
        return adminMapper.deleteByExample(null);
    }


}
